package com.feiyuan.generator.controller;

import com.feiyuan.common.result.R;
import com.feiyuan.generator.pojo.entity.DataSourceEntity;
import com.feiyuan.generator.service.DataSourceService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName DataSourceController
 * @Description 数据源配置控制器
 * @Author Xupf xupengfei131276@126.com
 * @Date 2021/9/30 9:45
 * @Version 1.0
 */
@RestController
//@CrossOrigin
@RequestMapping("/generator/dataSource")
public class DataSourceController {

    @Autowired
    private DataSourceService dataSourceService;

    /**
     * 查询所有数据源
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) {
        IPage<DataSourceEntity> dataSourceEntityIPage = dataSourceService.listPage(pageNo, pageSize);
        return R.ok().data("pages", dataSourceEntityIPage);
    }

    /**
     * 保存或者修改
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody DataSourceEntity dataSourceEntity) {
        return R.ok().data("obj", dataSourceService.saveOrUpdateDataSource(dataSourceEntity));
    }

    /**
     * 查询单个信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable Long id) {
        return R.ok().data("info", dataSourceService.getById(id));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/remove/{id}")
    public R remove(@PathVariable Long id) {
        return R.ok().data("remove", dataSourceService.removeById(id));
    }

    /**
     * 测试数据库连接
     * @param id
     * @return
     */
    @PostMapping("/testDataSource/{id}")
    public R testDataSource(@PathVariable Long id) {
        Boolean result = dataSourceService.testDataSource(id);
        return R.ok().data("result", result);
    }
}
